package com.ruoyi.system.domain.vo;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import lombok.Data;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


@Data
public class AutoBaseEntity {


    protected HashMap<String, Object> fields;

    /**
     * 非类属性，填充到fields中
     *
     * @param data
     */
    public void genField(Map<String, Object> data) {
        try {
            DateConverter converter = new DateConverter();
            converter.setPattern("yyyy-MM-dd HH:mm:ss");
            ConvertUtils.register(converter, Date.class);

            Field[] fields = getClass().getDeclaredFields();
            this.fields = new HashMap<String, Object>();

            Iterator<Map.Entry<String, Object>> iterator = data.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> entry = iterator.next();
                String key = entry.getKey();
                Object value = entry.getValue();

                boolean isField = false;
                Field temp = null;
                for (Field field : fields) {
                    field.setAccessible(true);
                    if (field.getName().equals(key)) {
                        isField = true;
                        temp = field;
                        break;
                    }
                }
                // 属于文件
                if (isField) {
                    if(temp.getName().equals("id")) {
                        temp.set(this, Long.parseLong(value.toString()));
                    } else {
                        temp.set(this, ConvertUtils.convert(value, temp.getType()));
                    }
                } else {
//                    String _key = StringUtils.toUnderScoreCase(key);
                    this.fields.put(key, value);
                }
            }
        }  catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 转成驼峰map
     * @return
     */

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            Field[] fields = getClass().getDeclaredFields();

            for (Field field : fields) {
                field.setAccessible(true);
                String fname = field.getName();
                Object fvalue = field.get(this);
                map.put(StringUtils.toCamelCase(fname), fvalue);
            }

            Iterator<Map.Entry<String, Object>> iterator = this.fields.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> entry = iterator.next();
                String key = entry.getKey();
                Object value = entry.getValue();
                map.put(key, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
